var express = require('express');
var router = express.Router();

var mysql = require('mysql');
		var conn = mysql.createConnection({
			host:'localhost',
			user:'root',
			password:'123456',
			database: 'projects',
			port: 3306
		});
	conn.connect();

router.get('/', function(req, res, next) {conn.query("select * from catalog",function(err,rows,fields){
					if (err) throw err;
					var catalogs = rows
				conn.query("select *,articles.id as aid from articles join catalog on articles.cata_id = catalog.id limit 10",function(err,rows,fields){
					if (err) throw err;
  res.render('index', { title:'liuyan',items:rows,catalog:catalogs,cata_id:0});
		});
	});
});
router.get('/news', function(req, res, next) {conn.query("select * from catalog",function(err,rows,fields){
					console.log(req.query)
					if (err) throw err;
					var catalogs = rows
				conn.query("select * from articles join catalog on articles.cata_id = catalog.id where articles.cata_id="+req.query.id+" limit 10",function(err,rows,fields){
					if (err) throw err;
  res.render('list', { title:'liuyan',items:rows,catalog:catalogs,cata_id:req.query.id});
		});
	});
});
router.get('/article', function(req, res, next) {
	conn.query("select * from catalog",function(err,rows,fields){
		if (err) throw err;
		var catalogs = rows
		conn.query("select * from articles join catalog on articles.cata_id = catalog.id where articles.cata_id="+req.query.id+" limit 10",function(err,rows,fields){
			if (err) throw err;
			console.log(rows[0])
  res.render('article', { title:'liuyan',article:rows[0],catalog:catalogs,cata_id:rows[0].cata_id});
		});
	});
});
router.get('/new', function(req, res, next) {
  res.render('users', { title:'登陆'});
		});


module.exports = router;
